import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mxapp/custom/CustomCategory.dart';
import 'package:mxapp/custom/CustomSwiper.dart';
import 'package:mxapp/custom/CustomTopTitle.dart';
import 'package:mxapp/custom/CustomBottomNavigationBar.dart' as bar;

class HeroApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MainScreen();
  }
}

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        bottomNavigationBar: bar.bars,
        appBar: AppBar(
          elevation: 5,
          title: TopTitle(),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
            padding: EdgeInsets.only(
              left: 8,
              right: 8,
            ),
            scrollDirection: Axis.vertical,
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                CustomSwiper(),
                CustomCategory(),
                Container(
                  margin: EdgeInsets.only(top: 10.0,bottom: 10),
                  height: 60,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(15),
                      color: Colors.black38,
                      image: DecorationImage(
                          image: NetworkImage(
                              'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3276446183,4258702571&fm=26&gp=0.jpg'),
                          fit: BoxFit.fill)),
                  child: InkWell(
                    onTap: () {
                      print('您点击了广告');
                    },
                  ),
                ),
                CustomScrollView(
                  slivers: <Widget>[
                    const SliverAppBar(
                      floating:false,
                      pinned: true,
                      snap: false,
                      flexibleSpace: FlexibleSpaceBar(
                        title: Text('Demo'),
                      ),
                    ),
                    SliverGrid(
                      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 200.0,
                        mainAxisSpacing: 10.0,
                        crossAxisSpacing: 10.0,
                        childAspectRatio: 4.0,
                      ),
                      delegate: SliverChildBuilderDelegate(
                            (BuildContext context, int index) {
                          return Container(
                            alignment: Alignment.center,
                            color: Colors.teal[100 * (index % 9)],
                            child: Text('grid item $index'),
                          );
                        },
                        childCount: 20,
                      ),
                    ),
                    SliverFixedExtentList(
                      itemExtent: 50.0,
                      delegate: SliverChildBuilderDelegate(
                            (BuildContext context, int index) {
                          return Container(
                            alignment: Alignment.center,
                            color: Colors.lightBlue[100 * (index % 9)],
                            child: Text('list item $index'),
                          );
                        },
                      ),
                    ),
                  ],
                )
                /*Container(
                  height: 200,
                  alignment: Alignment.center,
                  child: GestureDetector(
                    child: Hero(
                      tag: 'imageHero',
                      child: Image.network(
                        'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584339075495&di=10d7992089fb75bb6f8fb4d69635e3ff&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F64%2F76%2F20300001349415131407760417677.jpg',
                      ),
                    ),
                    onTap: () {
                      Navigator.push(context, MaterialPageRoute(builder: (_) {
                        return DetailScreen();
                      }));
                    },
                  ),
                ),*/
              ],
            )));
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List list = List();
  ScrollController _scrollController;

  @override
  void initState() {
    super.initState();
    _refresh();
    _scrollController = ScrollController()
      ..addListener(() {
        //判断是否滑到底
        if (_scrollController.position.pixels ==
            _scrollController.position.maxScrollExtent) {
          _loadMore();
        }
      });
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  Future _loadMore() async {
    await Future.delayed(Duration(seconds: 2), () {
      setState(() {
        list.addAll(
            List.generate(Random().nextInt(5) + 1, (i) => 'more Item $i'));
      });
    });
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 3), () {
      setState(() {
        list = List.generate(Random().nextInt(20) + 15, (i) => 'Item $i');
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: RefreshIndicator(
          displacement: 1.0,
          child: list == null || list.isEmpty
              ? Center(
                  child: CircularProgressIndicator(),
                )
              : ListView.builder(
                  controller: _scrollController,
                  itemCount: list.length + 1,
                  itemBuilder: (context, index) {
                    if (index == list.length) {
                      return Padding(
                        padding: const EdgeInsets.all(10.0),
                        child: Center(
                          child: CircularProgressIndicator(),
                        ),
                      );
                    }
                    return ListTile(
                      title: Text(list[index]),
                    );
                  }),
          onRefresh: _refresh),
    );
  }
}
